package com.ysxsoft.common_base.view.widgets;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.snackbar.Snackbar;
import com.ysxsoft.common_base.R;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.ysxsoft.common_base.view.custom.FlowLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MultipleFlowLayout extends FrameLayout {
    public OnDataSelectListener listener;
    private List<Data> data = new ArrayList<>();
    private FlowLayout flowLayout;
    private Context mContext;
    private List<TextView> dataViews = new ArrayList<>();

    public MultipleFlowLayout(@NonNull Context context) {
        this(context, null);
    }

    public MultipleFlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;
        flowLayout = new FlowLayout(mContext);
        flowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        addView(flowLayout);
    }

    public List<Data> getSelectedData() {
        List<Data> selectData = new ArrayList<>();
        List<String> d = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                d.add(data.get(i).getCid());
                selectData.add(data.get(i));
            }
        }
        return selectData;
    }

    /**
     * 返回选择成功的id  逗号隔开
     *
     * @return
     */
    public String getSelectedIds() {
        List<Data> selectData = new ArrayList<>();
        List<String> d = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                d.add(data.get(i).getCid());
                selectData.add(data.get(i));
            }
        }
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < d.size(); i++) {
            if (i == (d.size() - 1)) {
                s.append(d.get(i));
            } else {
                s.append(d.get(i) + ",");
            }
        }
        return s.toString();
    }

    public void initFlowLayout(List<Data> datas) {
        flowLayout.removeAllViewsInLayout();
        dataViews.clear();
        data.clear();
        data.addAll(datas);
        for (int i = 0; i < data.size(); i++) {
            View view = View.inflate(mContext, R.layout.item_pop_tab_theme_menu, null);
            TextView content = view.findViewById(R.id.content);
            content.setText(data.get(i).getCname());
            content.setSelected(data.get(i).isSelected());
            view.setTag(i);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Data d = data.get((Integer) v.getTag());
                    TextView textView = dataViews.get((Integer) v.getTag());
                    textView.setSelected(!d.isSelected());
                    d.setSelected(!d.isSelected());
                }
            });
            dataViews.add(content);
            flowLayout.addView(view);
        }
    }

    /**
     * 初始化flowLayout
     *
     * @param datas
     * @param maxSelectedCount
     */
    public void initFlowLayout(List<Data> datas, int maxSelectedCount) {
        flowLayout.removeAllViewsInLayout();
        dataViews.clear();
        data.clear();
        data.addAll(datas);
        for (int i = 0; i < data.size(); i++) {
            View view = View.inflate(mContext, R.layout.item_pop_tab_theme_menu, null);
            TextView content = view.findViewById(R.id.content);
            content.setText(data.get(i).getCname());
            content.setSelected(data.get(i).isSelected());
            view.setTag(i);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<Data> selectedData = getSelectedData();
                    String cid = datas.get((Integer) v.getTag()).getCid();
                    boolean exists = false;
                    for (int j = 0; j < selectedData.size(); j++) {
                        if (selectedData.get(j).getCid().equals(cid)) {
                            exists = true;
                        }
                    }
                    if (selectedData.size() == maxSelectedCount) {
                        if (!exists) {
                            ToastUtils.shortToast(mContext, "最多选" + maxSelectedCount + "个");
                            return;
                        } else {
                            //可以继续操作
                        }
                    }
                    Data d = data.get((Integer) v.getTag());
                    TextView textView = dataViews.get((Integer) v.getTag());
                    textView.setSelected(!d.isSelected());
                    d.setSelected(!d.isSelected());
                }
            });
            dataViews.add(content);
            flowLayout.addView(view);
        }
    }

    /**
     * 自定义View  未测试
     *
     * @param datas
     * @param listener
     */
    public void initCustomViewFlowLayout(List<Data> datas, int layoutId, int textViewId, OnDataSelectListener listener) {
        flowLayout.removeAllViewsInLayout();
        dataViews.clear();
        data.clear();
        data.addAll(datas);
        for (int i = 0; i < data.size(); i++) {
            View view = View.inflate(mContext, layoutId, null);
            TextView content = view.findViewById(textViewId);
            content.setText(data.get(i).getCname());
            content.setSelected(data.get(i).isSelected());
            view.setTag(i);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Data d = data.get((Integer) v.getTag());
                    TextView textView = dataViews.get((Integer) v.getTag());
                    textView.setSelected(!d.isSelected());
                    d.setSelected(!d.isSelected());
                    if (listener != null) {
                        listener.onDataChanged(getSelectedData(), getSelectedIds());
                    }
                }
            });
            dataViews.add(content);
            flowLayout.addView(view);
        }
    }

    public void initCustomViewFlowLayoutNoListener(List<Data> datas, int layoutId, int textViewId) {
        flowLayout.removeAllViewsInLayout();
        dataViews.clear();
        data.clear();
        data.addAll(datas);
        for (int i = 0; i < data.size(); i++) {
            View view = View.inflate(mContext, layoutId, null);
            TextView content = view.findViewById(textViewId);
            content.setText(data.get(i).getCname());
            content.setSelected(data.get(i).isSelected());
            view.setTag(i);
            dataViews.add(content);
            flowLayout.addView(view);
        }
    }

    public void setListener(OnDataSelectListener listener) {
        this.listener = listener;
    }

    public static class Data {
        private String cid;
        private String cname;
        private boolean isSelected;

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        public String getCid() {
            return cid == null ? "" : cid;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public String getCname() {
            return cname == null ? "" : cname;
        }

        public void setCname(String cname) {
            this.cname = cname;
        }
    }

    public FlowLayout getFlowLayout() {
        return flowLayout;
    }

    public interface OnDataSelectListener {
        void reset();

        void onDataChanged(List<Data> data, String id);
    }


    public List<Data> getDebugData() {
        List<Data> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Data d = new Data();
            d.setCid("" + i);
            if (i == 0) {
                d.setCname("数据" + i);
            } else if (i == 2) {
                d.setCname("长长数据" + i);
            } else {
                d.setCname("数据" + i);
            }
            d.setSelected(false);
            data.add(d);
        }
        return data;
    }

    public List<Data> getDebugTrueData() {
        List<Data> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Data d = new Data();
            d.setCid("" + i);
            if (i == 0) {
                d.setCname("数据" + i);
            } else if (i == 2) {
                d.setCname("长长数据" + i);
            } else {
                d.setCname("数据" + i);
            }
            d.setSelected(true);
            data.add(d);
        }
        return data;
    }
}
